package com.linsir.lin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.base.SearchPara;
import com.linsir.core.BaseResponse;
import com.linsir.core.CodeEnum;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.RoleDTO;
import com.linsir.lin.mapper.RoleMapper;
import com.linsir.lin.pojo.AccountRole;
import com.linsir.lin.pojo.BaseCode;
import com.linsir.lin.pojo.Role;
import com.linsir.lin.service.AccountService;
import com.linsir.lin.service.BaseCodeService;
import com.linsir.lin.service.RoleService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl  extends ServiceImpl<RoleMapper, Role> implements IService<Role>, RoleService {


    @Autowired
    private BaseCodeService baseCodeService;

    @Autowired
    private AccountService accountService;


    /**
     * 列表
     *
     * @param page
     * @param limit
     * @param searchParams
     * @return
     */
    @Override
    public GridDTO<RoleDTO> grid(String page, String limit, String searchParams) throws IOException, InvocationTargetException, IllegalAccessException {

        Map<String,String> map = SearchPara.toMap(searchParams);

        QueryWrapper<Role> wrapper=new QueryWrapper<Role>();

        if (map!=null)
        {
            wrapper.like("name",map.get("name"));
        }

        Page<Role> pageDto = new PageDto<Role>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);

        List<RoleDTO> roleDTOList = toDtoList(pageDto.getRecords());
        return new GridDTO<RoleDTO>("0","成功",pageDto.getSize(),roleDTOList);
    }

    /**
     * 根据账号寻找角色列表
     *
     * @param accountRoles
     * @return
     */
    @Override
    public List<Role> roles(List<AccountRole> accountRoles) {

        if (accountRoles!=null)
        {
            List<Integer> roleIds = new ArrayList<Integer>();

            for (AccountRole accountRole :accountRoles)
            {
                Integer roleId =accountRole.getRoleId();
                roleIds.add(roleId);
            }
            return listByIds(roleIds);
        }
        else
        {
            return null;
        }

    }

    @Override
    public List<BaseCode> roleGroup() {
        return baseCodeService.list("group_name");
    }



    @Override
    public List<RoleDTO> getAccountAndCheck(Integer accountId) throws InvocationTargetException, IllegalAccessException {
        List<Role> roles = accountService.getDTObyId(accountId).getRoleList();
        List<Role> allroles = allRole();
        List<RoleDTO> checkRoles = new ArrayList<RoleDTO>();
        for (Role role : allroles)
        {
            RoleDTO roleDTO = toDto(role);
            if (roles.contains(role))
            {
                roleDTO.setCheck("checked");
            }
            checkRoles.add(roleDTO);
        }

        return checkRoles;
    }

    @Override
    public List<Role> allRole() {
        return list();
    }

    /**
     * @param role
     * @return
     */
    @Override
    public RoleDTO toDto(Role role) throws InvocationTargetException, IllegalAccessException {

        RoleDTO roleDTO =new RoleDTO();
        BeanUtils.copyProperties(roleDTO,role);
        String groupName = baseCodeService.codeName("group_name",role.getGroup());
        roleDTO.setGroupName(groupName);
        return roleDTO;
    }

    /**
     * 转化成list
     *
     * @param roleList
     * @return
     */
    @Override
    public List<RoleDTO> toDtoList(List<Role> roleList) throws InvocationTargetException, IllegalAccessException {
        List<RoleDTO> roleDTOList =new ArrayList<RoleDTO>();
        for (Role role : roleList)
        {
            RoleDTO roleDTO= toDto(role);
            roleDTOList.add(roleDTO);
        }
        return  roleDTOList;
    }

    @Override
    public BaseResponse<Boolean> addOrUpdate(Role role) {
        boolean data = saveOrUpdate(role);
        if (data)
        {
            return new BaseResponse<Boolean>(CodeEnum.SUCCESS,"账户增加成果",data);
        }
        else
        {
            return new BaseResponse<Boolean>(CodeEnum.FAILED,"账户增加失败",data);
        }
    }

}
